home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 September / macformat-004.iso / Shareware City / Graphics / VideoToolbox ƒ / VideoToolboxSources / PrintfGWorld.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-07  |  2.8 KB  |  102 lines  |  [TEXT/KAHL]

  1. /*
  2. PrintfGWorld.c
  3.  
  4.     void PrintfGWorld(GWorldPtr our);
  5. Uses "printf" to print out the GWorld as a bitmap, representing blank as '.' and
  6. non-blank as '#'. The origin is designated '+'. Uses the upper lefthand pixel
  7. as the definition of blank (a poor but convenient assumption). This is very crude, 
  8. and intended only for debugging.
  9.  
  10.     void PrintStringAsBitmap(unsigned char *s);
  11. Uses DrawString to render the string in a GWorld, and then calls PrintfGWorld to
  12. render the GWorld onto the console. For debugging.
  13.  
  14. HISTORY:
  15. 1/17/94 dgp wrote it.
  16. 6/18/94 dgp Call SetGWorld(GetMainDevice())) before calling printf.
  17. 6/22/94 dgp Fixed bug: I was "restoring" to a GWorldPtr that was never initialized.
  18. */
  19. #include <VideoToolbox.h>
  20. void PrintfGWorld(GWorldPtr our);
  21. void PrintStringAsBitmap(unsigned char *s);
  22.  
  23. void PrintfGWorld(GWorldPtr our)
  24. // Print GWorld on the console as a bitmap.
  25. // Assume upper lefthand pixel is blank, which is rendered as '.'. 
  26. // Any other pixel value is represented as '#'.
  27. {
  28.     Rect r;
  29.     register unsigned long *pix,blank;
  30.     register int i,n;
  31.     int x,y;
  32.     char *bit;
  33.     GDHandle oldDevice;
  34.     long qD=0;
  35.   
  36.     Gestalt(gestaltQuickdrawVersion,&qD);
  37.     if(qD>=gestalt8BitQD){
  38.         oldDevice = GetGDevice();
  39.         SetGDevice(GetMainDevice());    // needed for printf
  40.     }
  41.     r=our->portRect;
  42.     r.left=0;
  43.     #if THINK_C
  44.         r.right=console_options.ncols;
  45.     #else
  46.         r.right=72;
  47.     #endif
  48.     CenterRectInRect(&r,&our->portRect);
  49.     SectRect(&r,&our->portRect,&r);
  50.     n=r.right-r.left;
  51.     bit=(char *)malloc((1+n)*sizeof(char));
  52.     if(bit==NULL)PrintfExit("PrintStringAsBitmap: Couldn't allocate %n bytes.\n",n+1);
  53.     bit[n]=0;
  54.     pix=(unsigned long *)malloc(n*sizeof(unsigned long));
  55.     if(pix==NULL)PrintfExit("PrintStringAsBitmap: Couldn't allocate %ld bytes.\n"
  56.         ,n*sizeof(long));
  57.     GetWindowPixelsQuickly((WindowPtr)our,our->portRect.left,our->portRect.top,pix,1);
  58.     blank=pix[0];
  59.     for(y=r.top;y<r.bottom;y++){
  60.         GetWindowPixelsQuickly((WindowPtr)our,r.left,y,pix,n);
  61.         for(i=0;i<n;i++){
  62.             if(pix[i]!=blank) bit[i]='#';                    // non-blank
  63.             else bit[i]='.';                                // blank
  64.         }
  65.         if(y==0)bit[-r.left]='+';                            // the origin
  66.         printf("%s\n",bit);
  67.     }
  68.     free(pix);
  69.     free(bit);
  70.     if(qD>=gestalt8BitQD)SetGDevice(oldDevice);
  71. }
  72.  
  73. void PrintStringAsBitmap(unsigned char *s)
  74. {
  75.     GWorldPtr our,old;
  76.     GDHandle oldDevice;
  77.     FontInfo f;
  78.     Rect r;
  79.     OSErr error;
  80.  
  81.     // Draw string into a new GWorld
  82.     GetFontInfo(&f);
  83.     SetRect(&r,0,-f.ascent,StringWidth(s),f.descent);    // nominal size
  84.     InsetRect(&r,-(f.widMax/2+2),-(f.leading+2));        // add margin
  85.     error=NewGWorld(&our,1,&r,NULL,NULL,0);
  86.     if(error)PrintfExit("PrintStringAsBitmap: NewGWorld error %d.\n",error);
  87.     GetGWorld(&old,&oldDevice);
  88.     SetGWorld(our,NULL);
  89.     TextFace(old->txFace);
  90.     TextFont(old->txFont);
  91.     TextSize(old->txSize);
  92.     EraseRect(&our->portRect);
  93.     MoveTo(0,0);
  94.     DrawString(s);
  95.     SetGWorld(old,oldDevice);
  96.     
  97.     PrintfGWorld(our);
  98.     
  99.     DisposeGWorld(our);
  100. }
  101.  
  102.